<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('geofences', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 围栏基本信息
            $table->string('name', 100);
            $table->string('code', 50)->unique();
            $table->text('description')->nullable();

            // 围栏类型
            $table->string('type', 20); // circle, polygon, point
            $table->string('coordinate_system', 20)->default('wgs84'); // wgs84, gcj02, bd09

            // 圆形围栏参数（当type=circle时使用）
            $table->decimal('center_latitude', 10, 8)->nullable();
            $table->decimal('center_longitude', 11, 8)->nullable();
            $table->integer('radius_meters')->nullable(); // 半径（米）

            // 多边形围栏参数（当type=polygon时使用）
            $table->json('polygon_points')->nullable(); // 多边形顶点坐标数组

            // 定点签到参数（当type=point时使用）
            $table->decimal('point_latitude', 10, 8)->nullable();
            $table->decimal('point_longitude', 11, 8)->nullable();

            // 围栏配置
            $table->integer('tolerance_meters')->default(50); // 误差范围（米）
            $table->boolean('is_active')->default(true);
            $table->time('effective_start_time')->nullable();
            $table->time('effective_end_time')->nullable();
            $table->json('effective_days')->nullable(); // 生效星期几 [1,2,3,4,5]

            // 适用范围
            $table->unsignedBigInteger('department_id')->nullable();
            $table->string('department_name', 100)->nullable();
            $table->unsignedBigInteger('position_id')->nullable();
            $table->string('position_name', 100)->nullable();
            $table->json('applicable_employees')->nullable();

            // 围栏地址信息
            $table->string('address', 200)->nullable();
            $table->string('province', 50)->nullable();
            $table->string('city', 50)->nullable();
            $table->string('district', 50)->nullable();

            // 统计信息
            $table->integer('usage_count')->default(0);
            $table->datetime('last_used_at')->nullable();

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['type']);
            $table->index(['is_active']);
            $table->index(['department_id']);
            $table->index(['position_id']);
            $table->index(['center_latitude', 'center_longitude']);
            $table->index(['code']);

            // 外键约束
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('position_id')->references('id')->on('positions')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('geofences');
    }
};
